﻿using System;
using System.Windows.Automation;

namespace QuickUIT.Controls
{
    /// <summary>
    /// Represents a tab control 
    /// </summary>
    public class UITab : UIBaseControl
    {
        /// <summary>
        /// Gets whether the tab control allows more than one child element to be selected concurrently.
        /// </summary>
        public new bool CanSelectMultiple
        {
            get { return base.CanSelectMultiple; }
        }

        /// <summary>
        /// Gets whether the tab control requires at least one child element to be selected.
        /// </summary>
        public new bool IsSelectionRequired
        {
            get { return base.IsSelectionRequired; }
        }

        /// <summary>
        /// Gets a value which indicates whether the control can scroll horizontally. 
        /// </summary>
        public new bool HorizontallyScrollable
        {
            get { return base.HorizontallyScrollable; }
        }

        /// <summary>
        /// Gets a value which indicates whether the control can scroll horizontally. 
        /// </summary>
        public new bool VerticallyScrollable
        {
            get { return base.VerticallyScrollable; }
        }

        /// <summary>
        /// Gets the value of the horizontal scroll position.
        /// If the control does not support the <see cref="ScrollPattern"/>, returns NaN. 
        /// </summary>
        public new double HorizontalScrollPercent
        {
            get { return base.HorizontalScrollPercent; }
        }

        /// <summary>
        /// Gets the value of the vertical scroll position 
        /// If the control does not support the <see cref="ScrollPattern"/>, returns NaN.
        /// </summary>
        public new double VerticalScrollPercent
        {
            get { return base.VerticalScrollPercent; }
        }

        /// <summary>
        /// Gets the value of the horizontal size of the viewable region.
        /// If the control does not support the <see cref="ScrollPattern"/>, returns NaN.
        /// </summary>
        public new double HorizontalViewSize
        {
            get { return base.HorizontalViewSize; }
        }

        /// <summary>
        /// Gets the value of the vertical size of the viewable region.
        /// If the control does not support the <see cref="ScrollPattern"/>, returns NaN.
        /// </summary>
        public new double VerticalViewSize
        {
            get { return base.VerticalViewSize; }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UITab"/> class. 
        /// </summary>
        public UITab()
            : base(ControlType.Tab)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITab"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        public UITab(UIBaseControl parent)
            : base(parent, ControlType.Tab)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITab"/> class.
        /// </summary>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UITab(string locator)
            : base(null, ControlType.Tab, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITab"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UITab(UIBaseControl parent, string locator)
            : base(parent, ControlType.Tab, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITab"/> class.
        /// </summary>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UITab(AutomationElement control) : base(control, ControlType.Tab)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITab"/> class.
        /// </summary>
        /// <param name="parent">The parent of the control</param>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UITab(UIBaseControl parent, AutomationElement control) : base(parent, control, ControlType.Tab)
        {
        }

        /// <summary>
        /// Selects a tab by name. 
        /// </summary>
        /// <param name="tabName">The name of the tab to select.</param>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public void SelectTab(string tabName)
        {
            WaitForParentWindowToBeReady();
            var criteria = new AutomationElementSearchCriteria(); 
            criteria.Add(new AutomationElementExactSearchCondition(AutomationElementIdentifiers.ControlTypeProperty, ControlType.TabItem));
            var tabs = AutomationElementFinder.FindAll(Control, criteria); 
            foreach (var tab in tabs) 
            {
                if (tab.Current.Name == tabName) 
                {
                    AutomationElementPatterns.Select(tab); 
                    return; 
                }
            }
        }

        /// <summary>
        /// Gets a collection of elements that represents the current selection for an element.
        /// </summary>
        /// <returns>A collection of selected elements.</returns>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new AutomationElement[] GetSelection()
        {
            return base.GetSelection(); 
        }

        /// <summary>
        /// Scrolls the visible region of the content area horizontally and vertically. 
        /// </summary>
        /// <param name="horizontalAmount">The horizontal scrolling increment that is specific to the control.</param>
        /// <param name="verticalAmount">The vertical scrolling increment that is specific to the control.</param>
        public new void Scroll(ScrollAmount horizontalAmount, ScrollAmount verticalAmount)
        {
            base.Scroll(horizontalAmount, verticalAmount);
        }

        /// <summary>
        /// Sets the horizontal and vertical scroll position as a percentage of the total content area within the control. 
        /// </summary>
        /// <param name="horizontalPercent">
        /// The percentage of the total horizontal content area. 
        /// <see cref="ScrollPattern.NoScroll"/> should be passed in if the control cannot be scrolled in this direction.
        /// </param>
        /// <param name="verticalPercent">
        /// The percentage of the total vertical content area. 
        /// <see cref="ScrollPattern.NoScroll"/> should be passed in if the control cannot be scrolled in this direction.
        /// </param>
        public new void SetScrollPercent(double horizontalPercent, double verticalPercent)
        {
            base.SetScrollPercent(horizontalPercent, verticalPercent);
        }
    }
}
